package com.njtech.pet.dao.example;

import com.njtech.pet.constant.Constant;
import com.njtech.pet.dao.MessageDao;
import com.njtech.pet.dao.PersonDao;
import com.njtech.pet.entity.Message;
import com.njtech.pet.entity.User;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.List;
import java.util.Scanner;

/**
 * @author lxj
 * Date 2020-03-14 16:57
 * Description 测试MessageDao
 */
public class MessageDaoExample {
    private static MessageDao messageDao;
    private static Scanner scanner;

    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("spring-dao.xml");
        messageDao = (MessageDao) context.getBean("messageDao");

        System.out.println("准备开始测试" + messageDao.toString());

        scanner = new Scanner(System.in);

        //start flag -1 end flag 0
        int num = -1;
        System.out.println("please enter test index:");
        System.out.println("0: exit,");
        System.out.println("1: test insert,");
        System.out.println("2: test update,");
        System.out.println("3: test select.");
        while (num != 0) {
            num = scanner.nextInt();
            switch (num) {
                case 0:
                    System.out.println("bye.");break;
                case 1:
                    insert();
                    break;
                case 2:
                    update();
                    break;
                case 3:
                    select();
                    break;
                default:
                    System.err.println("undefined index, please enter again.");
            }
            System.out.println("waiting for your command: ");
        }
    }

    private static void select() {
        String a;
        System.out.println("sender id:");
        int sid = scanner.nextInt();
        User sender = new User();
        sender.setId(sid);
        System.out.println("receiver id:");
        int rid = scanner.nextInt();
        User receiver = new User();
        receiver.setId(rid);

        List<Message> messages;
        System.out.println("select without date");
        messages = messageDao.selectByCoupleUsersBetweenDates(sender, receiver, null, null);
        for (Message temp:
             messages) {
            System.out.println(temp);
        }
        a = scanner.next();
        String beginDate = "2020-03-14";
        String endDate = "2020-03-14 17:15:10";

        System.out.println("select with begin date");
        messages = messageDao.selectByCoupleUsersBetweenDates(sender, receiver, beginDate, null);
        for (Message temp:
                messages) {
            System.out.println(temp);
        }
        a = scanner.next();

        System.out.println("select with end date");
        messages = messageDao.selectByCoupleUsersBetweenDates(sender, receiver, null, endDate);
        for (Message temp:
                messages) {
            System.out.println(temp);
        }
        a = scanner.next();

        System.out.println("select between dates");
        messages = messageDao.selectByCoupleUsersBetweenDates(sender, receiver, beginDate, endDate);
        for (Message temp:
                messages) {
            System.out.println(temp);
        }
    }

    private static void update() {
        System.out.println("message id:");
        int id = scanner.nextInt();
        Message message = new Message(id, null, null, null, null, Constant.Message.STATE_READ.getValue(), null);
        int row = messageDao.updateMessageStateByMessageId(message);
        System.out.println(row + " updated.");
    }

    private static void insert() {
        System.out.println("sender id:");
        int sid = scanner.nextInt();
        User sender = new User();
        sender.setId(sid);
        System.out.println("receiver id:");
        int rid = scanner.nextInt();
        User receiver = new User();
        receiver.setId(rid);
        Message message = new Message(null, null, sender, receiver, "insertedMessage from "+sid+" to " + rid, Constant.Message.STATE_UNREAD.getValue(), Constant.Message.TYPE_CHAT.getValue());
        messageDao.insert(message);
        System.out.println("inserted.");

    }

    @Before
    public void prepare(){
        ApplicationContext context = new ClassPathXmlApplicationContext("spring-dao.xml");
        messageDao = (MessageDao) context.getBean("messageDao");
        System.out.println("prepared");
    }

    @Test
    public void selectMessageShow(){
        User user = new User();
        user.setId(2);
        System.out.println(messageDao.selectReceiverMessageShow(user,"未读"));
    }
}
